var modifyOrAddFlag;
// 初始化数据
var reuseDegreeData = [ {
	valueCode : '',
	valueName : '全部'
}, {
	valueCode : '0',
	valueName : '0'
}, {
	valueCode : '1',
	valueName : '1'
}, {
	valueCode : '2',
	valueName : '>1'
} ];
$(document)
		.ready(
				function() {

					$('#win').window('close');
					// 重用度下拉框
					$('#comboxReuseDegree').combobox({
						valueField : 'valueCode',
						textField : 'valueName',
						editable : false,
						value : "0",
						data : reuseDegreeData,
						onSelect : function(record) {
						}
					});
					// 接口类型下拉框
					$('#comboxIfType').combobox(
							{
								url : BASE_URL_DATA_DICTIONARY_VALUE_SEARCH
										+ '/findByTypeCode?typeCode='
										+ DDTC_INTERFACE_TYPE + '&v='
										+ Math.random(),
								valueField : 'valueCode',
								textField : 'valueName',
								editable : false,
								method : 'get',
								loadFilter : function(data) {
									var allCheck = {
										'valueCode' : '',
										'valueName' : '全部'
									};
									data.unshift(allCheck);
									return data;
								}
							});

					$("#datagrid")
							.datagrid(
									{
										url : BASE_URL_API
												+ '/serviceReuseDegree/search/findServiceReuseDegreeInfo?v='
												+ Math.random(),
										method : 'get',
										autoRowHeight : true,
										singleSelect : true,
										striped : true,
										nowrap : false,
										pageSize : 20,
										pageList : [ 10, 20, 50, 100 ],
										columns : [ [
												{
													field : 'fieldName',
													title : '所属领域',
													width : 30
												},
												{
													field : 'systemName',
													title : '所属系统',
													width : 30
												},
												{
													field : 'ifName',
													title : '接口名称',
													width : 115,
													align : 'center',
													formatter : function(value,
															rowData, rowIndex) {
														return '<a href="javascript:void(0)" style="padding-bottom: 0px;margin-bottom: 50px"    onclick="showDetial('
																+ rowIndex
																+ ')">'
																+ value
																+ '</a>';
													}
												},
												{
													field : 'ifType',
													title : '接口类型',
													width : 30,												
													formatter : function(value,
															rowData, rowIndex) {
														return value == '1' ? "WEBSERVICE"
																: "REST";
													}
												},

												{
													field : 'createTime',
													title : '接口注册时间',
													width : 40
												},
												{
													field : 'version',
													title : '接口版本',
													width : 30
												},
												{
													field : 'resueAmount',
													title : '服务重用度',
													width : 35
												},
												{
													field : 'reuseSystemName',
													title : '重用系统',
													width : 35,
													formatter : function(value,
															rowData, rowIndex) {
														if (value == null
																|| value == '') {
															return "";
														}
														var strs = value
																.split(",");
														var companent = '<lable>';
														$
																.each(
																		strs,
																		function(
																				i,
																				item) {
																			if(i==0){
																				companent += item;
																			}else{
																				companent += ','+item;
																			}
																		});
														companent += '</lable>';
														return companent;
													}

												}, {
													field : 'remark',
													title : '备注',
													width : 60													
												} ] ],
										pagination : true,
										onSelect : function(rowIndex, rowData) {
										},
										onUnselect : function() {
										},
										loadFilter : function(data) {
											// alert("inData:"+JSON.stringify(data));
											return {
												total : data.totalElements,
												rows : data.content
											};
										},
										onBeforeLoad : function(param) {
											param.page -= 1;
											param.size = param.rows;
											delete param.rows;

											param.sort = 'createTime,desc';

											var ifType = $('#comboxIfType')
													.combobox('getValue');
											if (ifType != "") {
												param.ifType = ifType;
											}
											var reuseDegree = $(
													'#comboxReuseDegree')
													.combobox('getValue');
											if (reuseDegree != "") {
												param.reuseDegree = reuseDegree;
											}
											var ifName = $('#txtIfName').val();
											if (ifName != "") {
												param.ifName = ifName;
											}

											var startDateTime = $(
													"#txtStartDateTime")
													.datebox('getValue');
											if (startDateTime != "") {
												param.startDateTime = startDateTime;
											}
											var endDateTime = $(
													"#txtEndDateTime").datebox(
													'getValue');
											if (endDateTime != "") {
												param.endDateTime = endDateTime;
											}
										}
									});

					$("#datagrid").datagrid('getPager').pagination({
						displayMsg : '当前显示从 [{from}] 到 [{to}] 共[{total}]条记录'
					});
					$(".panel-tool-close, #instance_cancel").click(function(e) {
						$("body").css("overflow-x", "inherit")
					});

					$("#datagrid4ReuseSystem")
							.datagrid(
									{
										url : BASE_URL_API
												+ '/serviceReuseDegree/search/findServiceReuseSystemInfo?v='
												+ Math.random(),
										method : 'get',
										autoRowHeight : true,
										singleSelect : true,
										striped : true,
										nowrap : false,
										pageSize : 20,
										pageList : [ 10, 20, 50, 100 ],
										columns : [ [ {
											field : 'systemName',
											title : '系统名称',
											width : 70
										}, {
											field : 'userAccount',
											title : '帐号',
											width : 30,
											align : 'center'
										}, {
											field : 'userName',
											title : '姓名',
											width : 30,
											align : 'center'
										} ] ],
										pagination : true,
										onSelect : function(rowIndex, rowData) {
										},
										onUnselect : function() {
										},
										loadFilter : function(data) {
											// alert("inData:"+JSON.stringify(data));
											return {
												total : data.totalElements,
												rows : data.content
											};
										},
										onBeforeLoad : function(param) {
											param.page -= 1;
											param.size = param.rows;
											delete param.rows;

											param.sort = 'createTime,desc';

											var ifId = $('#txtIfId').val();
											if (ifId != "") {
												param.ifId = ifId;
											}
										}
									});

					$("#datagrid4ReuseSystem")
							.datagrid('getPager')
							.pagination(
									{
										displayMsg : '当前显示从 [{from}] 到 [{to}] 共[{total}]条记录'
									});

				});

// 查询
function search() {
	$('#datagrid').datagrid('reload');
}
function refresh() {
	$('#datagrid').datagrid('load');
}

function showDetial(rowIndex) {
	var row = $('#datagrid').datagrid('getData').rows[rowIndex];
	var interfaceId = row.ifId;
	var interfaceName= row.ifName;
	var versions = row.version;
	var title = "服务浏览";
	var url = "/pages/esb/ServiceManagement/businessServiceView.html?interfaceId="
			+ window.encodeURI(interfaceId)+"&interfaceName="+window.encodeURI(interfaceName)+ '&v=' + Math.random();;
	var icon = $(this).attr("icon");
	window.parent.OpenTab(title, url, icon, true);
}
function showReuseSystem(rowIndex) {
	var row = $('#datagrid').datagrid('getData').rows[rowIndex];
	$('#txtIfId').val(row.ifId);
	$('#win').window('setTitle', '查看重用系统');
	$('#win').window('open');
	$('#datagrid4ReuseSystem').datagrid('reload');
}